package main

import (
	"context"
	"fmt"
	process "github.com/mudler/go-processmanager"
	"github.com/rs/zerolog/log"
	"path/filepath"
	"shyxy-net/pkg/model"
	"time"
)

func main() {
	absModelPath, err := filepath.Abs("models")
	if err != nil {
		log.Error().Msgf("Error getting absolute path: %v", err)
	}
	ml := model.NewModelLoader(absModelPath, true)
	if !ml.ExistsInModelPath("qwen3-max.json") {
		log.Info().Msgf("qwen3-max.json not exists!")
	}
	userId := 10012
	modelName := "qwen3-max"
	modelId := fmt.Sprintf("%d_%s", userId, modelName)
	rpcBackend, err := ml.Load(
		model.WithModel("qwen3-max.json"),
		model.WithModelID(modelId),
		model.WithBackendString("rpc_server_qwen"),
		model.EnableParallelRequests,
		model.WithAssetDir(""),
	)
	if err != nil {
		log.Error().Msgf("Error loading model: %v", err)
	}
	time.Sleep(10 * time.Second)
	check, err := rpcBackend.HealthCheck(context.Background())
	if err != nil {
		return
	}

	modelsPre := ml.ListModels()
	for _, m := range modelsPre {
		log.Info().Msgf("Model: %s", m.ID)
	}

	log.Info().Msgf("RPC Service Ready: %v", check)
	if err := ml.StopGRPC(func(id string, p *process.Process) bool {
		return id == modelId
	}); err != nil {
		log.Error().Msgf("Error stopping model: %v", err)
		return
	}

	modelsAft := ml.ListModels()
	for _, m := range modelsAft {
		log.Info().Msgf("Model: %s", m.ID)
	}

}
